package com.dayuan.controller;

import java.io.File;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import com.dayuan.bean.Goods;
import com.dayuan.bean.MerchantInfo;
import com.dayuan.constant.ConstantCode;
import com.dayuan.service.GoodsService;
import com.dayuan.service.MerchantInfoService;
import com.dayuan.utils.pageHelper;

@Controller
@RequestMapping("/adminGM")
public class AdminGoodsController {

	private static Logger logger = LoggerFactory.getLogger(AdminGoodsController.class);

	@Resource
	private MerchantInfoService merchantInfoService;

	@Resource
	private GoodsService goodsService;

	/**
	 * 通过商家关键字模糊查询 ， 若传入值为null，则查询出所有商家信息
	 * 
	 * @param merchantName
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/merchantsList.shtml", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView merchantsList(MerchantInfo merchantInfo, Integer pageIndex, ModelMap modelMap) {
		try {

			if (pageIndex == null) {
				pageIndex = 1;
			}
			// 查询总行数
			int totalCount = merchantInfoService.selectCount(merchantInfo);

			pageHelper pageHelper = new pageHelper(pageIndex, totalCount);

			List<MerchantInfo> merchants = merchantInfoService.selectMerchantByParam(merchantInfo,
					pageHelper.getStartIndex(), pageHelper.getPageNum());

			// 传给前端总页数和当前页商家信息
			modelMap.addAttribute("merchantsList", merchants);
			modelMap.addAttribute("pageHelper", pageHelper);
			modelMap.addAttribute("shopName", merchantInfo.getShopName());

			ModelAndView modelAndView = new ModelAndView("adminSelectMerchant", modelMap);

			return modelAndView;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(ConstantCode.FAIL.printMsg() + "," + e.getMessage());
		}
		return null;
	}

	/**
	 * 通过商品名称关键字 模糊查询商品信息集合, 若传入值为null，则查询出所有商品信息 若传入值为商品id 精确查询商品信息 添加分页功能
	 * 
	 * @param goodsName
	 * @param pageIndex
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/goodsList.shtml", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView goodsList(Goods goods, Integer pageIndex, ModelMap modelMap) {
		try {

			if (pageIndex == null) {
				pageIndex = 1;
			}
			// 查询总行数
			int totalCount = goodsService.selcetTotal(goods);

			pageHelper pageHelper = new pageHelper(pageIndex, totalCount);

			List<Goods> goodsList = goodsService.sellerSelectGoods(goods, pageHelper.getStartIndex(),
					pageHelper.getPageNum());
			// 传给前端总页数和当前页goods
			modelMap.addAttribute("goodsList", goodsList);
			modelMap.addAttribute("pageHelper", pageHelper);
			modelMap.addAttribute("goodsName", goods.getGoodsName());
			ModelAndView modelAndView = new ModelAndView("adminGoodsManage", modelMap);
			return modelAndView;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(ConstantCode.FAIL.printMsg() + "," + e.getMessage());
		}
		return null;
	}

	/**
	 * admin删除商品
	 * 
	 * @param goods
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/deleteGoods.shtml", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView deleteGoods(Goods goods, RedirectAttributesModelMap modelMap) {
		try {
			int count = goodsService.deleteGoodsByParam(goods);
			if (count < 1) {
				modelMap.addFlashAttribute("deleteMsg", "商品删除失败！!");
			} else {
				modelMap.addFlashAttribute("deleteMsg", "商品删除成功！!");
			}
			return new ModelAndView("redirect:/adminGM/goodsList.shtml", modelMap);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(ConstantCode.FAIL.printMsg() + "," + e.getMessage());
		}
		return null;
	}

	/**
	 * admin 添加 商品
	 * 
	 * @param goods
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/addGoods", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView addGoods(Goods goods, RedirectAttributesModelMap modelMap) {
		try {
			int count = goodsService.insertGoodsSelective(goods);
			if (count < 1) {
				modelMap.addFlashAttribute("addMsg", "添加商品失败！");
			} else {
				modelMap.addFlashAttribute("addMsg", "添加商品成功！");
			}
			return new ModelAndView("redirect:/adminGM/toAdd.shtml", modelMap);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(ConstantCode.FAIL.printMsg() + "," + e.getMessage());
		}
		return null;
	}

	/**
	 * admin 更新 商品
	 * 
	 * @param goods
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/updateGoods", method = {RequestMethod.GET, RequestMethod.POST })
	public ModelAndView updateGoods(Goods goods, RedirectAttributesModelMap modelMap) {
		try {
			int count = goodsService.updateGoodsByParams(goods);
			if (count < 1) {
				modelMap.addFlashAttribute("updateMsg", "商品信息更新失败....");
			} else {
				modelMap.addFlashAttribute("updateMsg", "商品信息更新成功！");
			}
			return new ModelAndView("redirect:/adminGM/toUpdate.shtml", modelMap);

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(ConstantCode.FAIL.printMsg() + "," + e.getMessage());
		}
		return null;
	}
	
	@RequestMapping(value = "/toUpdate.shtml", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toUpdate() {
		ModelAndView modelAndView = new ModelAndView("adminUpdateGoods");
		return modelAndView;
	}
	
	@RequestMapping(value = "/toAdd.shtml", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAdd() {
		ModelAndView modelAndView = new ModelAndView("adminAddGoods");
		return modelAndView;
	}
	
	@RequestMapping(value = "/toStart.shtml", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView start() {
		ModelAndView modelAndView = new ModelAndView("adminSelectMerchant");
		return modelAndView;
	}

}
